<template>
  <div class="hello">
      <h1>{{num}}</h1>
      <button @click="commitfn">点击我触发commit函数</button>
      <button @click="reduceNumfn">点击我触发action给num--</button>
      <button @click="getusersreturn">点击我获取mutations里的返还值</button>
      <button @click="getdatareturn">点击我获取actions里的返还值</button>
  </div>
</template>

<script>
// actions和mutations的区别？
// actions里面可以处理异步逻辑  mutations不能处理异步逻辑
// 网络请求 actions来进行请求
// actions会返还promise对象   ，mutations 里的函数是没有返还值的
import {mapMutations,mapState,mapActions} from 'vuex'
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  created:function(){
      // 获取state里的num值
      console.log(this.$store.state.num);
  },
  methods:{
    ...mapMutations(['addNum']),
    ...mapActions(['reduceNum']),
    commitfn(){
        // this.$store.commit("addNum",2);
       this.addNum(2);
    },
    // 可以把num做减法
    reduceNumfn(){
      // dispatch:触发 actions里的函数
      // commit 会触发mutations里的函数
        // this.$store.dispatch("reduceNum",3);
       this.reduceNum(3);
    },
    getusersreturn(){
      // mutations里的函数没有返还值
      let res =  this.$store.commit("getusers");
      console.log(res);
    },
    async getdatareturn(){
      let {data} = await this.$store.dispatch("getdatareturn");
      console.log(data);
    }
  },
  computed:{
    ...mapState(['num'])
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
